<?php 
/**
 * 前台注册页面
 * @author machao
 * @created 2011-6-25
 */
define('_VIEW_MODEL_','default');

require_once("./init.php");
if(!empty($_POST)){ // post提交 插入数据
	$formData = $_POST;
	$formData['phone'] = trim($formData['phone']);
	$formData['name']  = trim($formData['name']); 
	//验证手机	
	if(!isPhoneNum($formData['phone'])){		
		errorPrompt('phone_formate_err','regist.php');exit;
	}
	//验证name
	if(strlen($formData['name'])<=0 || strlen($formData['name'])>30){
		errorPrompt('name_lenght_err','regist.php');exit;
	}
	//验证password
	if(strlen($formData['password'])<6 || strlen($formData['name'])>16){
		errorPrompt('password_lenght_err','regist.php');exit;
	}
	$checkSql = "select * from {$telper}user where phone = '".$formData['phone']."' || name = '".$formData['name']."'";
	$result   = $db->getRow($checkSql);
	/**验证用户名是否被注册**/
	if(isset($result['name']) && ($result['name'] == $formData['name']) ){
		errorPrompt('name_have_regist','regist.php');exit;
	}
	/**验证手机是否被注册**/
	if(isset($result['phone']) && ($result['phone'] == $formData['phone']) ){
		errorPrompt('phone_have_regist','regist.php');exit;
	}
	$md5_psw = md5($password);
	$sql = "insert into {$telper}user (name,password,phone,headimage,lasttime,created)values('$name','$md5_psw','$phone','./userheadimg/0.gif','$timestamp','$timestamp')";
	try{
		$db->Execute($sql);
		$userInf = $db->getRow("select id,name,phone from {$telper}user where phone = '".$formData['phone']."'");
		empty($userInf)?errorPrompt('failure','regist.php'):setLoginSession($userInf['id']);
		redirect('/index.php');
	}catch(Exception $error){
		errorPrompt('failure','/regist.php');	
	}		
}else{
	header("Content-type:text/vnd.wap.wml;charset=utf-8");
	$smartyObj->display('regist.tpl');
}
?>

